﻿using System;
using System.Linq;
using System.Collections;
using System.Configuration;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;
using Microsoft.VisualBasic;

namespace GONES.Tools.Common
{
    /// <summary>
    /// 页面工具类
    /// </summary>
    public static class Http
    {
        /// <summary>
        /// 弹出对话框
        /// </summary>
        /// <param name="Message">提示信息,例子："不能为空!"</param>
        public static void Pagealert(string Message)
        {
            string script = "<script language=\"javascript\">\r\n" +
                "   alert(\"" + Message + "\");\r\n" +
                "</script>";

            HttpContext.Current.Response.Write(script);
        }

        /// <summary>
        /// 弹出对话框并返回上一页
        /// </summary>
        /// <param name="message"></param>
        public static void PagealertThenBack(string message)
        {
            string script = "<script language=\"javascript\">\r\n" +
                    "   alert(\"" + message + "\");\r\n" +
                    "   history.go(-1);\r\n" +
                    "</script>";

            HttpContext.Current.Response.Write(script);
            HttpContext.Current.Response.End();

        }

        ///// <summary>
        ///// 服务器端弹出alert对话框，并使控件获得焦点
        ///// </summary>
        ///// <param name="ControlName">获得焦点控件Id值,比如：txt_Name</param>
        ///// <param name="Message">提示信息,例子："请输入您姓名!"</param>
        ///// <param name="page">Page类</param>
        //public static void Alert(string ControlName, string Message)
        //{
        //    string script = "<script language=\"javascript\">\r\n" +
        //        "   alert(\"" + Message + "\");\r\n" +
        //        "   document.getElementById(\"" + ControlName + "\").focus();\r\n" +
        //        "</script>";

        //    Page.Response.Write(script);
        //}

        /// <summary>
        /// 弹出对话框，刷新页面
        /// </summary>
        /// <param name="Message">消息</param>
        public static void PagealertThenSkip(string Message)
        {
            string script = "<script language=\"javascript\">\r\n" +
                "   alert(\"" + Message + "\");\r\n" +
                "   window.parent.location.reload();\r\n" +
                "</script>";

            HttpContext.Current.Response.Write(script);
            HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 弹出对话框，后页面跳转
        /// </summary>
        /// <param name="Message">消息</param>
        /// <param name="Url">URL地址</param>
        public static void PagealertThenSkip(string Message, string Url)
        {
            string script = "<script language=\"javascript\">\r\n" +
                "   alert(\"" + Message + "\");\r\n" +
                "   window.location.href = \"" + Url + "\";\r\n" +
                "</script>";

            HttpContext.Current.Response.Write(script);
            HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 弹出对话框，后页面跳转
        /// </summary>
        /// <param name="Message">消息</param>
        /// <param name="Url">URL地址</param>
        public static void PagealertThenSkipparent(string Message, string Url)
        {
            string script = "<script language=\"javascript\">\r\n" +
                "   alert(\"" + Message + "\");\r\n" +
                "   window.parent.location.href = \"" + Url + "\";\r\n" +
                "</script>";

            HttpContext.Current.Response.Write(script);
            HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 弹出对话框，js代码
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="javascript">注入js代码</param>
        public static void PageScript(string message, string javascript)
        {
            string script = "<script language=\"javascript\">\r\n" +
                "   alert(\"" + message + "\");\r\n" +
                "   " + javascript + "\r\n" +
                "</script>";

            //page.ClientScript.RegisterStartupScript(this.GetType(), "", script);

            HttpContext.Current.Response.Write(script);
            //HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 向页面注册 js 脚本
        /// </summary>
        /// <param name="javascript">js 脚本</param>
        /// <param name="page">页面类</param>
        public static void PageRegscript(string javascript, System.Web.UI.Page page)
        {
            javascript = "<script language=\"javascript\">\n" + javascript + "\n</script>";
            page.ClientScript.RegisterStartupScript(page.GetType(), "", javascript);
        }




        /// <summary>
        /// 判断当前页面是否接收到了Post请求
        /// </summary>
        /// <returns>是否接收到了Post请求</returns>
        public static bool IsPost()
        {
            return HttpContext.Current.Request.HttpMethod.Equals("POST");
        }
        /// <summary>
        /// 判断当前页面是否接收到了Get请求
        /// </summary>
        /// <returns>是否接收到了Get请求</returns>
        public static bool IsGet()
        {
            return HttpContext.Current.Request.HttpMethod.Equals("GET");
        }

        /// <summary>
        /// 返回指定的服务器变量信息
        /// </summary>
        /// <param name="strName">服务器变量名</param>
        /// <returns>服务器变量信息</returns>
        public static string GetServerString(string strName)
        {
            //
            if (HttpContext.Current.Request.ServerVariables[strName] == null)
            {
                return "";
            }
            return HttpContext.Current.Request.ServerVariables[strName].ToString();
        }

        /// <summary>
        /// 返回上一个页面的地址
        /// </summary>
        /// <returns>上一个页面的地址</returns>
        public static string GetUrlReferrer()
        {
            string retVal = null;

            try
            {
                retVal = HttpContext.Current.Request.UrlReferrer.ToString();
            }
            catch { }

            if (retVal == null)
                return "";

            return retVal;

        }

        /// <summary>
        /// 得到当前完整主机头
        /// </summary>
        /// <returns>完整主机头</returns>
        public static string GetCurrentFullHost()
        {
            HttpRequest request = HttpContext.Current.Request;
            if (!request.Url.IsDefaultPort)
            {
                return string.Format("{0}:{1}", request.Url.Host, request.Url.Port.ToString());
            }
            return request.Url.Host;
        }

        /// <summary>
        /// 得到主机头
        /// </summary>
        /// <returns>主机头</returns>
        public static string GetHost()
        {
            return HttpContext.Current.Request.Url.Host;
        }


        /// <summary>
        /// 获取当前请求的原始 URL(URL 中域信息之后的部分,包括查询字符串(如果存在))
        /// </summary>
        /// <returns>原始 URL</returns>
        public static string GetRawUrl()
        {
            return HttpContext.Current.Request.RawUrl;
        }

        /// <summary>
        /// 判断当前访问是否来自浏览器软件
        /// </summary>
        /// <returns>当前访问是否来自浏览器软件</returns>
        public static bool IsBrowserGet()
        {
            string[] BrowserName = { "ie", "opera", "netscape", "mozilla" };
            string curBrowser = HttpContext.Current.Request.Browser.Type.ToLower();
            for (int i = 0; i < BrowserName.Length; i++)
            {
                if (curBrowser.IndexOf(BrowserName[i]) >= 0)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 判断是否来自搜索引擎链接
        /// </summary>
        /// <returns>是否来自搜索引擎链接</returns>
        public static bool IsSearchEnginesGet()
        {
            string[] SearchEngine = { "google", "yahoo", "msn", "baidu", "sogou", "sohu", "sina", "163", "lycos", "tom" };
            string tmpReferrer = HttpContext.Current.Request.UrlReferrer.ToString().ToLower();
            for (int i = 0; i < SearchEngine.Length; i++)
            {
                if (tmpReferrer.IndexOf(SearchEngine[i]) >= 0)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 获得当前完整Url地址
        /// </summary>
        /// <returns>当前完整Url地址</returns>
        public static string GetUrl()
        {
            return HttpContext.Current.Request.Url.ToString();
        }


        /// <summary>
        /// 获得指定Url参数的值
        /// </summary>
        /// <param name="strName">Url参数</param>
        /// <returns>Url参数的值</returns>
        public static string GetQueryString(string strName)
        {
            if (HttpContext.Current.Request.QueryString[strName] == null)
            {
                return "";
            }
            return HttpContext.Current.Request.QueryString[strName];
        }

        /// <summary>
        /// 获得当前页面的名称
        /// </summary>
        /// <returns>当前页面的名称</returns>
        public static string GetPageName()
        {
            string[] urlArr = HttpContext.Current.Request.Url.AbsolutePath.Split('/');
            return urlArr[urlArr.Length - 1].ToLower();
        }

        /// <summary>
        /// 返回表单或Url参数的总个数
        /// </summary>
        /// <returns></returns>
        public static int GetParamCount()
        {
            return HttpContext.Current.Request.Form.Count + HttpContext.Current.Request.QueryString.Count;
        }


        /// <summary>
        /// 获得指定表单参数的值
        /// </summary>
        /// <param name="strName">表单参数</param>
        /// <returns>表单参数的值</returns>
        public static string GetFormString(string strName)
        {
            if (HttpContext.Current.Request.Form[strName] == null)
            {
                return string.Empty;
            }
            return HttpContext.Current.Request.Form[strName];
        }

        /// <summary>
        /// 获得Url或表单参数的值, 先判断Url参数是否为空字符串, 如为True则返回表单参数的值
        /// </summary>
        /// <param name="strName">参数</param>
        /// <returns>Url或表单参数的值</returns>
        public static string GetString(string strName)
        {
            if ("".Equals(GetQueryString(strName)))
            {
                return GetFormString(strName);
            }
            else
            {
                return GetQueryString(strName);
            }
        }


        /// <summary>
        /// 获得指定Url参数的int类型值
        /// </summary>
        /// <param name="strName">Url参数</param>
        /// <returns>Url参数的int类型值</returns>
        public static int GetQueryInt(string strName)
        {
            return GetQueryInt(strName, -1);
        }

        /// <summary>
        /// 获得指定Url参数的int类型值
        /// </summary>
        /// <param name="strName">Url参数</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>Url参数的int类型值</returns>
        public static int GetQueryInt(string strName, int defValue)
        {
            return Tools.StrToInt(HttpContext.Current.Request.QueryString[strName], defValue);
        }


        ///// <summary>
        ///// 获得指定表单参数的int类型值
        ///// </summary>
        ///// <param name="strName">表单参数</param>
        ///// <returns>表单参数的int类型值</returns>
        //public static int GetFormInt(string strName)
        //{
        //    return GetFormInt(strName, -1);
        //}

        /// <summary>
        /// 获得指定表单参数的int类型值
        /// </summary>
        /// <param name="strName">表单参数</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>表单参数的int类型值</returns>
        public static int GetFormInt(string strName, int defValue)
        {
            return Tools.StrToInt(HttpContext.Current.Request.Form[strName], defValue);
        }

        /// <summary>
        /// 获得指定表单参数的bool类型值
        /// </summary>
        /// <param name="strName">表单参数</param>
        /// <returns>表单参数的bool类型值</returns>
        public static bool GetFormBool(string strName)
        {
            return Tools.StrToBool(HttpContext.Current.Request.Form[strName], false);
        }

        /// <summary>
        /// 获得指定Url或表单参数的int类型值, 先判断Url参数是否为缺省值, 如为True则返回表单参数的值
        /// </summary>
        /// <param name="strName">Url或表单参数</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>Url或表单参数的int类型值</returns>
        public static int GetInt(string strName, int defValue)
        {
            if (GetQueryInt(strName, defValue) == defValue)
            {
                return GetFormInt(strName, defValue);
            }
            else
            {
                return GetQueryInt(strName, defValue);
            }
        }

        /// <summary>
        /// 获得指定Url参数的float类型值
        /// </summary>
        /// <param name="strName">Url参数</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>Url参数的int类型值</returns>
        public static float GetQueryFloat(string strName, float defValue)
        {
            return Tools.StrToFloat(HttpContext.Current.Request.QueryString[strName], defValue);
        }


        /// <summary>
        /// 获得指定表单参数的float类型值
        /// </summary>
        /// <param name="strName">表单参数</param>
        /// <returns>表单参数的float类型值</returns>
        public static float GetFormFloat(string strName)
        {
            return GetFormFloat(strName, -1);
        }

        /// <summary>
        /// 获得指定表单参数的float类型值
        /// </summary>
        /// <param name="strName">表单参数</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>表单参数的float类型值</returns>
        public static float GetFormFloat(string strName, float defValue)
        {
            return Tools.StrToFloat(HttpContext.Current.Request.Form[strName], defValue);
        }

        /// <summary>
        /// 获得指定表单参数的decimal类型值
        /// </summary>
        /// <param name="strName"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        public static decimal GetFormDecimal(string strName, decimal defValue)
        {
            return Tools.StrToDecimal(HttpContext.Current.Request.Form[strName], defValue);
        }

        /// <summary>
        /// 获得指定Url或表单参数的float类型值, 先判断Url参数是否为缺省值, 如为True则返回表单参数的值
        /// </summary>
        /// <param name="strName">Url或表单参数</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>Url或表单参数的int类型值</returns>
        public static float GetFloat(string strName, float defValue)
        {
            if (GetQueryFloat(strName, defValue) == defValue)
            {
                return GetFormFloat(strName, defValue);
            }
            else
            {
                return GetQueryFloat(strName, defValue);
            }
        }

        /// <summary>
        /// 获得当前页面客户端的IP
        /// </summary>
        /// <returns>当前页面客户端的IP</returns>
        public static string GetIP()
        {


            string result = String.Empty;

            result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }

            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.UserHostAddress;
            }

            if (null == result || result == String.Empty || !Tools.IsIP(result))
            {
                return "0.0.0.0";
            }

            return result;

        }
    }
}
